
/**
 * Created by winny on 16/3/7.
 */
var xhr = {
    // urlPre:'http://192.168.156.144:8000/h5',
    //urlPre:'http://192.168.60.53:8080/h5Bx',
    // urlPre:'http://zhou.17wanxiao.com:8666/h5',
    // urlPre: 'http://192.168.156.144:8003/h5',
    // urlPre:'/repairPro/h5',
    // urlPre: 'http://39.106.126.22/gkshop',
    // urlPre: 'http://zyb.iycgg.com/api/majorcategories',
    // urlPre: '/gkshop',
    // token: this.getSearch('token') || '',
    // batchUrl: 'http://zyb.iycgg.com/api/scores/province',

    onepointlineUrl: 'http://zyb.iycgg.com/api/onepointline',
 
    onepointline: function (param, callback) {
        var url = this.onepointlineUrl + '?province=' + param.province + '&subject=' + param.subject + '&year=' + param.year
        this.sendReq(url, {}, callback)
    },
    sendReq: function (url, param, callback) {
        var self = this;
        return $.ajax({
            url: url,
            method: 'GET',
            cache: false,
            data: param,
            dataType: 'json',
            success: function (result) {
                // console.log(result)
                callback && callback(result)
                myApp.hideIndicator();
            },
            error: function (xhr, status) {
                console.log(xhr)
                console.log(status)

                myApp.alert("服务器异常,请稍后再试吧");
                myApp.hideIndicator();
            },
        });
    },
    sendRequest: function (url, param, callback) {
        console.log("请求地址：" + url + "\n请求参数：" + JSON.stringify(param));
        var self = this;
        // console.log(self.urlPre + url);
        return $.ajax({
            //param['token'] = "58adf245-56f8-493b-a99a-9920d664dc07"
            url: self.urlPre + url,
            method: 'POST',
            cache: false,
            data: param,
            dataType: 'json',
            // beforeSend: function (xhr) {
            //     xhr.setRequestHeader('token', self.getSearch('token'))
            // },
            success: function (result) {
                // console.log(result);
                if (result.code == '0000') {
                    callback && callback(result, result.data);
                } else {
                    myApp.alert(result.message_ + "(" + result.code_ + ")");
                }
                myApp.hideIndicator();
            },
            error: function (xhr, status) {
                console.log(arguments)
                console.log(xhr);
                console.log(status)
                callback && callback(status)
                myApp.alert("服务器异常,请稍后再试吧");
                myApp.hideIndicator();
            },
        });
    },
    openPhotoBrowser: function (index, photoArr) {
        var myPhotoBrowser = myApp.photoBrowser({
            zoom: 400,
            photos: photoArr,
            initialSlide: index,
            ofText: '/',
            theme: 'light',
            type: 'standalone',
        });
        myPhotoBrowser.open(); // 打开图片浏览器
    },
    /**
     * 获取当前url查询字符串
     */
    init: function () {
        this._search = this.getUrlParams();
        //this.getLoginInfo(this.token);
    },
    /**
     * 获取url中查询的值
     * @param key
     * @returns {*}
     */
    getSearch: function (key) {
        if (!this._search) this._search = this.getUrlParams();

        if (!key) {
            return this._searchArgs;
        }
        return this._search[key];
    },
    /**
     * 返回当前 URL 的查询字串
     * @returns {*}
     */
    getUrlParams: function () {
        if (this._search) {
            return this._search;
        }
        var _s = window.location.search,
            attr = {};
        _s = decodeURI(decodeURI(_s));
        this._searchArgs = _s;
        if (_s.indexOf("?") >= 0) {
            _s = _s.substring(1);
            var arry = _s.split("&");
            for (var i = 0; i < arry.length; i++) {
                var item = arry[i];
                attr[item.split("=")[0]] = item.split("=")[1];
            }
        }
        return attr;
    },
};
xhr.init();